package com.heatup.apt.api.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.heatup.apt.api.service.DataDictService;
import com.heatup.apt.api.service.ProductServiceDetailService;
import com.heatup.apt.common.dao.OrderInfoMapper;
import com.heatup.apt.common.dao.ProductServiceDetailMapper;
import com.heatup.apt.common.util.DateUtils;
import com.heatup.apt.model.DataDict;
import com.heatup.apt.model.order.YearCardOrderLog;
import com.heatup.apt.model.product.PresaleProductServiceDetailApiModel;
import com.heatup.apt.model.product.ProductServiceDetail;
import com.heatup.apt.model.user.UserInfo;

/**
 *
 * ProductServiceDetail 表数据服务层接口实现类
 *
 */
@Service
public class ProductServiceDetailServiceImpl extends SuperServiceImpl<ProductServiceDetailMapper, ProductServiceDetail> implements ProductServiceDetailService {
	
	@Autowired
	private ProductServiceDetailMapper productServiceDetailMapper;
	@Autowired
	private OrderInfoMapper orderInfoMapper;
	@Autowired
	private DataDictService dataDictService;
	
	@Override
	public Map<String,Object> queryPresaleProductServiceDetailList() {
		Map<String,Object> resultMap = new HashMap<>();
		List<PresaleProductServiceDetailApiModel> list = productServiceDetailMapper.queryPresaleProductServiceDetailAll();
		resultMap.put("productDetailList", list);
		if(list.size() > 0) {
			Date activityEndTime = DateUtils.getDateShort(list.get(0).getActivityEndTime());
			Date toDate = DateUtils.getDateShort(new Date());
			if(activityEndTime.before(toDate)) activityEndTime = toDate;
			resultMap.put("activityEndTime", activityEndTime);
		}else {
			resultMap.put("activityEndTime", DateUtils.getDateShort(new Date()));
		}
		
		// 查询最大允许延长时间
		List<DataDict> dataDictList = dataDictService.queryDataDicts("MAX_OPED_CARD_DAY_NUM");
		if(dataDictList.size() > 0) {
			DataDict dataDict = dataDictList.get(0);
			resultMap.put("maxDelayDayNum", dataDict.getDictCode());
		}else {
			resultMap.put("maxDelayDayNum", 7);
		}
		return resultMap;
	}
	
	/**
	 * 获取年卡开单记录
	 */
	@Override
	public Map<String, Object> queryMyOpenYeadCardOrderLog(UserInfo userInfo, String payTime) {
		Map<String,Object> resultMap = new HashMap<>();
		List<YearCardOrderLog> yearCardlogList = orderInfoMapper.queryYearCardLogList(userInfo.getId(), payTime);
		if(yearCardlogList.size() > 0) {
			resultMap.put("totalOrderCount", yearCardlogList.get(0).getTotalOrderCount());
			resultMap.put("totalPayAmount", yearCardlogList.get(0).getTotalPayAmount());
		}else {
			resultMap.put("totalOrderCount", 0);
			resultMap.put("totalPayAmount", 0);
		}
		resultMap.put("yearCardOrderLog", yearCardlogList);
		return resultMap;
	}


}